import 'package:bookshelf/entity/score_entity.dart';

import '../http/api.dart';


class ResortEntity{


  Map<String,int> resortsByNo = {};
  Map<String,int> resortsById = {};

  static final ResortEntity _singleton = ResortEntity._internal();

  factory ResortEntity() {
    return _singleton;
  }

  ResortEntity._internal();

  void clear(){
    resortsByNo.clear();
    resortsById.clear();
  }

  void addByNo(no){
    if(resortsByNo.containsKey(no)){
      int count = resortsByNo[no]!;
      count++;
      resortsByNo[no] = count;
    }else{
      resortsByNo[no] = 1;
    }
  }

  void addById(id){
    if(resortsById.containsKey(id)){
      int count = resortsById[id]!;
      count++;
      resortsById[id] = count;
    }else{
      resortsById[id] = 1;
    }
  }


  void upload(){
    var list = [];

    if(resortsByNo.isNotEmpty){
      resortsByNo.forEach((key, value) {
        list.add(ScoreEntity()
          ..bookNo = key
          ..score =value
          ..toJson()
        );
      });
    }
    if(resortsById.isNotEmpty){
      resortsById.forEach((key, value) {
        list.add(ScoreEntity()
          ..bookId = key
          ..score =value
            ..toJson()
        );
      });
    }
    if(list.isNotEmpty){
      addScore(list);
    }
    clear();
  }


}